package com.obtk.service.impl;

import com.obtk.mapper.ProProduceMapper;
import com.obtk.mapper.ProStorageMapper;
import com.obtk.modelSGW.ProProduct;
import com.obtk.modelSGW.ProStorage;
import com.obtk.service.ProProduceService;
import com.obtk.util.ResultUtil;
import com.obtk.util.UUIDUtil;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-03-30 18:58:00
 */
@Service
public class ProProduceServiceImpl implements ProProduceService {
    @Autowired
    private ProProduceMapper proProduceMapper;
    @Autowired
    private ProStorageMapper proStorageMapper;

    @Override
    public List<ProProduct> selectProProduct(ProProduct proProduct) {
        return proProduceMapper.selectProProduct(proProduct);
    }

    @Override
    public List<ProProduct> selectProProduct1(ProProduct proProduct) {
        return proProduceMapper.selectProProduct1(proProduct);
    }

    @Override
    public ProProduct selectByProProductName(String prodName) {
        return proProduceMapper.selectByProdName(prodName);
    }

    @Override
    public ResponseVo register(ProProduct proProduct) {
        //获取一个随机值 订单  16位16进制码
        String[] split = UUID.randomUUID().toString().split("-");

        String code = UUIDUtil.getUniqueIdByUUId();
        if (null != proProduceMapper.selectByCode(code)) {
            return ResultUtil.error("产品编号已存在");
        } else {
            proProduct.setProdCode(code);
            int num = proProduceMapper.insert(proProduct);
            if (num > 0) {
                ProStorage proStorage = new ProStorage(proProduct.getProdCode(), proProduct.getProdName(),"未设置", proProduct.getProdUnit(), 0, proProduct.getProdMemo(), proProduct.getProdStatus());
                int i = proStorageMapper.insert(proStorage);
                if (i > 0) {
                    return ResultUtil.success("产品添加成功");
                } else {
                    return ResultUtil.error("产品添加失败");
                }
            } else {
                return ResultUtil.error("产品添加失败");
            }
        }
    }

    @Override
    public ProProduct selectByProProductId(String prodId) {
        return proProduceMapper.selectByProdId(prodId);
    }

    @Override
    public ResponseVo updateByProProductId(ProProduct proProduct) {
            if (proProduceMapper.updateByProdId(proProduct) > 0) {
                ProStorage proStorage = new ProStorage();
                proStorage.setStkProdCode(proProduct.getProdCode());
                proStorage.setStkStatus(proProduct.getProdStatus());
                if (proStorageMapper.updateByStkProdCode(proStorage) > 0) {
                    return ResultUtil.success("修改成功！");
                } else {
                    return ResultUtil.error("修改失败");
                }
            } else {
                return ResultUtil.error("修改失败");
            }
    }
    @Override
    public int updateStatusBatch(List<String> prodIds, Integer prodStatus) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("prodIds", prodIds);
        params.put("prodStatus", prodStatus);
        return proProduceMapper.updateStatusBatch(params);
    }
    @Override
    public ProProduct getProDuct(String proDuctCode){
        return proProduceMapper.getProDuct(proDuctCode);
    }
}
